<!--
 * @Author: Andy 9620487+andy_song@user.noreply.gitee.com
 * @Date: 2022-10-21 13:54:03
 * @LastEditors: Andy 9620487+andy_song@user.noreply.gitee.com
 * @LastEditTime: 2022-10-21 13:54:07
 * @FilePath: /vue_shop/code/src/components/Index.vue
 * @Description:主页结构
-->
<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
        <el-button class="logoutBtn" type="info" @click="logout"
          >退出</el-button
        >
      </div>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="togleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          unique-opened
          active-text-color="#409FFF"
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
          router
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="'/home/' + subItem.path"
              @click="saveNavState('/home/' + subItem.path)"
            >
              <template slot="title">
                <i :class="iconObj[subItem.id]"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { leftMenu } from "../http/api";
export default {
  name: "home",
  data() {
    return {
      //左侧菜单数据
      menuList: [],
      isCollapse: false,
      activePath:"",
      //图标数组
      iconObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
        110: "el-icon-user-solid",
        111: "el-icon-tickets",
        112: "el-icon-film",
        104: "el-icon-s-goods",
        115: "el-icon-star-off",
        121: "el-icon-delete-solid",
        107: "el-icon-s-promotion",
        146: "el-icon-s-flag",
      },
    };
  },

  components: {},

  computed: {},

  mounted() {},
  created() {
    this.getMyMenus();
    this.activePath=sessionStorage.getItem('activePath')
  },
  methods: {
    /**
     * @description: 退出登录
     * @return {*}
     */
    logout() {
      //清除本地token
      sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMyMenus() {
      const { data: res } = await leftMenu();
      if (res.meta.status != 200) return;
      this.menuList = res.data;
    },
    /**
     * @description: 按钮开关
     * @return {*}
     */
    togleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath){
      sessionStorage.setItem('activePath',activePath)
    }
  },
  watch: {},
  mixins: [],
};
</script>
<style  scoped lang="less">
.el-container {
  height: 100%;
}
.el-header {
  position: relative;
  width: 100%;
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      height: 40px;
    }
    span {
      margin-left: 15px;
    }
  }
}
.logoutBtn {
  position: absolute;
  right: 10px;
}
.el-aside {
  background-color: #333744;

  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  // 鼠标放上去变成小手
  cursor: pointer;
}
</style>

